<template>
  <div class="aside-container">
    <el-menu class="el-menu-vertical-demo"
             default-active="1"
             @open="handleOpen"
             background-color="#545c64"
             text-color="#fff"
             @close="handleClose"
             active-text-color="#ffd04b"
             :collapse="isCollapse">

      <h3>{{ isCollapse ? '后台' : '在线考试系统' }}</h3>
      <hr>

      <!--   没有子菜单的渲染   -->
      <el-menu-item v-for="item in noChildren" :key="item.menuName" @click="clickMenu(item)" :index="item.menuName">
        <i :class="`el-icon-${item.icon}`"></i>
        <span slot="title">{{ item.title }}</span>
      </el-menu-item>

      <!--   有子菜单的菜单的渲染   -->
      <el-submenu v-for="item in hasChildren" :key="item.menuName" :index="item.menuName">
        <template slot="title">
          <i :class="`el-icon-${item.icon}`"></i>
          <span slot="title">{{ item.title }}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item
              v-for="childItem in item.children"
              :key="childItem.menuName"
              @click="clickMenu(childItem)"
              :index="childItem.menuName">

            {{ childItem.title }}
          </el-menu-item>
        </el-menu-item-group>
      </el-submenu>

    </el-menu>
  </div>
</template>

<script>
import {mapState} from 'vuex'
import {getMenu} from "@/api/menu";

export default {
  name: "Aside",
  data() {
    return {
      menus: []
    };
  },
  methods: {
    handleOpen(key, keyPath) {

    },
    handleClose(key, keyPath) {

    },
    clickMenu(item) {
      if (this.$route.path !== item.path && !(this.$route.path === "/" && this.$route.path === '/home')) {
        this.$router.push({name: item.menuName})
      }
    }
  },
  computed: {
    ...mapState(['isCollapse']),
    noChildren() {
      return this.menus.filter(item => !item.children).filter(item => item.type != 'D')
    },
    hasChildren() {
      return this.menus.filter(item => item.children)
    }
  },
  created() {
    getMenu().then(res => {
      this.menus = res
    })
  }
}
</script>

<style scoped lang="less">
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

.el-menu {
  height: 100vh;
  border-right: none;

  h3 {
    color: #ffffff;
    text-align: center;
    font-size: 16px;
    font-weight: 400;
    margin-top: 20px;
    line-height: 30px;
    margin-bottom: 10px;
  }
}
</style>
